package com.sctu.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.R;
import com.sctu.adapter.PlayAmuseListviewAdapter;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.PlayAmuseHead;
import com.sctu.bean.PlayAmuseListOne;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhu on 2015/11/7.
 */
public class PlayAmuseFragment extends Fragment {
    private View view;
    private ViewPager viewPager;
    private ListView listView;
    private List<View> listViews;
    private ImageView imageView;
    private List<ImageView> listimageView = new ArrayList<ImageView>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        if (view == null){
            view = inflater.inflate(R.layout.fragment_play_amuse, container, false);
            viewPager = (ViewPager) view.findViewById(R.id.play_amuse_viewpager);
            listView = (ListView) view.findViewById(R.id.amuse_list);
            initHeading();
            initListView();
        }

        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }

        return view;
    }

    private void initHeading() {
        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_AMUSE_HEAD, listener, errorListener);
    }

    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String headJson) {
            Log.e("headJson", headJson);

            PlayAmuseHead playAmuseHead = JSON.parseObject(headJson, PlayAmuseHead.class);
            List<PlayAmuseHead.Results> ar = playAmuseHead.getResults();

            listViews = new ArrayList<View>();
            for (int i = 0; i < ar.size(); i++) {
                View itemviewpagerview = LayoutInflater.from(getActivity()).inflate(R.layout.item_viewpager_play_amuse, null);
                imageView = (ImageView) itemviewpagerview.findViewById(R.id.item_viewpager_paly_amuse_image);
                GetVolleyHttp.getInstance(getActivity()).setImageLoader(imageView, R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + ar.get(i).getPath());
                listViews.add(itemviewpagerview);
            }
            viewPager.setAdapter(new ViewPagerAdapter(listViews));

            /**
             * 对小圆点进行控制
             *
             */
            LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.play_amuse_head_Relative_spot);

            for (int i = 0; i < ar.size(); i++) {
                LinearLayout linearLayout = new LinearLayout(getActivity());
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                margin.setMargins(10, 0, 0, 0);
                 ImageView imageViews = new ImageView(getActivity());
                imageViews.setLayoutParams(new ViewGroup.LayoutParams(10, 10));
                listimageView.add(imageViews);
                if (i == 0) {
                    listimageView.get(i).setBackgroundResource(R.drawable.selected_hover);
                } else {
                    listimageView.get(i).setBackgroundResource(R.drawable.unselected_hover);
                }
                linearLayout.addView(listimageView.get(i), margin);
                mainLayout.addView(linearLayout);
            }

            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                /**
                 * 此方法是页面跳转完后得到调用，arg0是你当前选中的页面的Position（位置编号）
                 * @param position
                 */
                @Override
                public void onPageSelected(int position) {
                    //遍历让当前选中图片下的小圆点设置颜色
                    for (int i = 0; i < listimageView.size(); i++) {
                        listimageView.get(i)
                                .setBackgroundResource(R.drawable.scenic_tour_round_after_click_click);

                        if (position != i) {
                            listimageView.get(i)
                                    .setBackgroundResource(R.drawable.scenic_tour_round_after_click_unclick);
                        }
                    }
                }
                /**
                 * arg0 == 1的时辰默示正在滑动，arg0 == 2的时辰默示滑动完毕了，arg0 == 0的时辰默示什么都没做。
                 */
                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG).show();
        }
    };

    private void initListView() {
        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_AMUSE_LIST, listlistener, errorListener);
    }

    Response.Listener<String> listlistener = new Response.Listener<String>() {
        @Override
        public void onResponse(String listJson) {
            Log.e("listJson", listJson);

            PlayAmuseListOne playAmuseListOne = JSON.parseObject(listJson, PlayAmuseListOne.class);
            listView.setAdapter(new PlayAmuseListviewAdapter(getActivity(), playAmuseListOne.getResults()));
        }
    };

}














